package com.bowlong.third.payment.uc;

import java.io.IOException;
import java.util.Map;

import com.bowlong.json.JSON;
import com.bowlong.util.MapEx;
import com.bowlong.util.Ref;

// UC充值回调
public class PaymentUc {

	static final String FAIL = "FAILURE";
	static final String SUCC = "SUCCESS";

	/*
	 * { "data":{ "orderId":"abcf1330", "gameId":123, "serverId":654,
	 * "ucid":123456, "payWay":1, "amount":"100.00",
	 * "callbackInfo":"custominfo=xxxxx#user=xxxx", "orderStatus":"S",
	 * "failedDesc":"" }, "sign":"e49bd00c3cf0744c7049e73e16ae8acd" }
	 */
	public static boolean checkOrder(final String body, Ref<Double> money,
			Ref<Integer> orderId, Ref<String> errMsg, Ref<String> retToChannel)
			throws IOException {
		Map<?, ?> map_json = JSON.parseMap(body);
		// Map<String, List<String>> map = HttpParser.parseQueryString(body,
		// "UTF-8");
		if (map_json == null || map_json.isEmpty()) {
			retToChannel.val = FAIL;
			errMsg.val = "body == null || body.isEmpty()";
			return false;
		}
		Map<?, ?> map = MapEx.getMap(map_json, "data");
		if (map == null || map.isEmpty()) {
			retToChannel.val = FAIL;
			errMsg.val = "body.data == null || body.data.isEmpty()";
			return false;
		}

		double amount = MapEx.getDouble(map, "amount");

		// int billno = NumEx.stringToInt( MapEx.getString(map, "orderId"));
		// int billno = MapEx.getInt(map, "orderId"); // uc sdk 生成
		int billno = MapEx.getInt(map, "callbackInfo"); // 游戏端生成

		String status = MapEx.getString(map, "orderStatus");

		if (status.equals("S")) { // S-成功支付 F-支付失败
			money.val = amount;
			orderId.val = billno;
			retToChannel.val = SUCC;
			errMsg.val = "status: " + status + " is ok";
			return true;
		} else {
			money.val = amount;
			orderId.val = billno;
			retToChannel.val = SUCC;
			errMsg.val = "status: " + status + " is false";
			return false;
		}
	}
}
